//
//  MyEvaluationVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/9.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "MyEvaluationVC.h"
#import "MyEvaluationModel.h"
#import "MyEvaluationCell.h"
#import "EvaluationDetailVC.h"

@interface MyEvaluationVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) MyEvaluationCell *MyEvaluationCell;
@property (nonatomic,strong) UISegmentedControl *segment; //分段控制
@property (nonatomic,copy) NSString *strEvaluation; //投诉来源:0->投诉司机 1->投诉货主
@end

@implementation MyEvaluationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self setTitleName:CJLString(@"我的评价")];
    [self setBackLeftButton:@""];
    [self selectSendGoodsType];
    self.strEvaluation = @"0";
    self.MyEvaluationCell = [[MyEvaluationCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}


- (void)selectSendGoodsType{
   
    NSArray *array = [NSArray arrayWithObjects:@"我评价的",@"评价我的",nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake((SCREEN_WIDTH-LW(180))/2, LW(10) + kStatusBarHeight, LW(180), LW(25));
    [self.segment addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventValueChanged];
    [self.image addSubview:self.segment];
    //
  if (DeviceSystemVersion.doubleValue > 13) {
      UIColor *tintColor = [UIColor cjMainColor];
      UIImage *tintColorImage = [UIImage imageWithColor:[UIColor cjMainColor]];
      // Must set the background image for normal to something (even clear) else the rest won't work
      [self.segment setBackgroundImage:[UIImage imageWithColor:self.segment.backgroundColor ? self.segment.backgroundColor : [UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
      [self.segment setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
      [self.segment setBackgroundImage:[UIImage imageWithColor:[tintColor colorWithAlphaComponent:0.2]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
      [self.segment setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];
      [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor cjWhiteColor], NSFontAttributeName: [UIFont cjTitleFont14]} forState:UIControlStateSelected];
      [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor cjMainColor], NSFontAttributeName: [UIFont cjTitleFont14]} forState:UIControlStateNormal];
      [self.segment setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
      self.segment.layer.borderWidth = 0.5;
      self.segment.layer.borderColor = [tintColor CGColor];
      self.segment.selectedSegmentIndex = 0;
  }else{
      self.segment.tintColor = [UIColor cjMainColor];
  }
  self.segment.selectedSegmentIndex = 0;
}

//不同分段
-(void)changeType:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        DLog(@"--=-=-==-");
        self.strEvaluation = @"0";
    }else{
        DLog(@"676767676");
        self.strEvaluation = @"1";
    }
    
    WS(ws);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ws.refreshType = @"down";
        ws.page = 1;
        [ws.tableView.mj_header beginRefreshing];
    });
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/orderComment/myComment",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"commentType":self.strEvaluation
    };
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
//        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
            //            #import "CommonRouteModel.h"
            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }

            NSError * error ;
            NSArray *arr = [MyEvaluationModel arrayOfModelsFromDictionaries:list error:&error];
            [ws.modelArray addObjectsFromArray:arr];
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];

            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }

//        }else{
//
//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        
        [ws.tableView reloadData];

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"MyEvaluationCell";
    MyEvaluationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyEvaluationCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    MyEvaluationModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model isShippment:self.strEvaluation];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyEvaluationModel *model = self.modelArray[indexPath.row];
    EvaluationDetailVC *vc  = [[EvaluationDetailVC alloc]init];
    vc.strCode = model.orderCode;
    vc.strCommentType = self.strEvaluation;
    [self pushVc:vc];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}
@end
